package com.example.othree.common;

import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.*;

@Component
public class DataUtil {
    public ArrayList<Double> toList(String stringData){
        String[] stringList = stringData.split(",");

        // 创建一个ArrayList来存储Double类型的值
        ArrayList<Double> doubleList = new ArrayList<>();

        // 遍历字符串列表，将每个字符串转换为Double并添加到doubleList中
        for (String str : stringList) {
            try {
                // 使用Double.parseDouble来转换字符串为Double
                doubleList.add(Double.parseDouble(str));
            } catch (NumberFormatException e) {
                // 如果转换失败（虽然在这个例子中不太可能发生，因为所有字符串都是有效的数字），则处理异常
                System.err.println("无法将字符串 '" + str + "' 转换为Double");
                // 根据需要，你可以选择在这里抛出异常、记录日志或执行其他操作
            }
        }

        return doubleList;
    }

    public ArrayList<Double> calculateAverages(ArrayList<ArrayList<Double>> nestedList) {

        if (nestedList.isEmpty()) {
            throw new IllegalArgumentException("One of the sublists is empty, which is not allowed.");
        }

        int columnCount = nestedList.get(0).size(); // 假设所有行具有相同的列数
        ArrayList<Double> columnSums = new ArrayList<>(Collections.nCopies(columnCount, 0.0));

        // 遍历每一行和每一列
        for (List<Double> row : nestedList) {
            for (int col = 0; col < columnCount; col++) {
                columnSums.set(col, columnSums.get(col) + row.get(col));
            }
        }

        // 计算列平均
        ArrayList<Double> columnAverages = new ArrayList<>();
        for (double sum : columnSums) {
            columnAverages.add(sum / nestedList.size());
        }

        return columnAverages;
    }

    public String getTimeNew(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        // 获取系统当前时间
        Date date = new Date();

        // 使用SimpleDateFormat对象将时间格式化为指定格式
        String currentTime = sdf.format(date);

        return currentTime;
    }



    private double calculateDistance(float x1, float y1, float x2, float y2) {
        return Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
    }

    // 反距离权重插值算法
    public  Float[] idwInterpolation(List<Float[]> dataPoints, float inputX, float inputY, float power) {



        int dataLen = dataPoints.get(0).length;
        Float[] newDataPoint = new Float[dataLen];

        // 使用new关键字创建数组
        float[] sum = new float[dataLen - 2];
        for (int i = 0; i < sum.length; i++) {
            sum[i] = 0.0f;
        }
        float[] totalWeight = new float[dataLen - 2];
        for (int i = 0; i < sum.length; i++) {
            totalWeight[i] = 0.0f;
        }

        for (Float[] dataPoint : dataPoints) {
            for (int i = 2; i < dataLen; i++){
//            for (int i = 2; i < 3; i++){
                double distance = calculateDistance(dataPoint[0], dataPoint[1], inputX, inputY);
                double weight = Math.pow(1.0 / distance, power);
                if (distance == 0.0) {

                    System.arraycopy(dataPoint, 0, newDataPoint, 0, dataLen);
                    newDataPoint[0] = inputX;
                    newDataPoint[1] = inputY;
                    return newDataPoint; // 如果距离为0，直接返回该点的值
                }
                sum[i-2] += weight * dataPoint[i];
                totalWeight[i-2] += weight;
            }

        }

        for (int i = 2; i < dataLen; i++){
            newDataPoint[i] = sum[i-2] / totalWeight[i-2];
        }
        newDataPoint[0] = inputX;
        newDataPoint[1] = inputY;
        return newDataPoint;
    }

    public float[][] gridLonLat(float west, float east, float south, float north, int gridSize) {
        // 给定的区域边界
//        double west = 114.2; // 最西端经度
//        double east = 120.2; // 最东端经度
//        double south = 36.5; // 最南端纬度
//        double north = 42.5; // 最北端纬度

        // 网格的尺寸
//        int gridSize = 200; // 200x200网格
        float[][] grid = new float[2][gridSize * gridSize];

        // 计算每个网格单元的宽度和高度
        float deltaLongitude = (east - west) / (gridSize - 1);
        float deltaLatitude = (north - south) / (gridSize - 1);

        // 生成网格坐标并打印
        for (int i = 0; i < gridSize; i++) {
            float currentLatitude = north - i * deltaLatitude;
            for (int j = 0; j < gridSize; j++) {
                float currentLongitude = west + j * deltaLongitude;
                grid[1][i * gridSize + j] = currentLatitude;
                grid[0][i * gridSize + j] = currentLongitude;
            }
        }
        return grid;
    }

    public HashMap<String, Object> entityToMap(Object entity) throws IllegalAccessException {
        HashMap<String, Object> map = new HashMap<>();
        Class<?> clazz = entity.getClass();

        for (Field field : clazz.getDeclaredFields()) {
            field.setAccessible(true); // 确保私有属性也可以访问
            map.put(field.getName(), field.get(entity));
        }
        return map;
    }


}
